/*******************************
 *  КЛасс отвечающий за работу елемента screen
 *  Создает набор кадров, управляет их добавлением, удалением, перемещением
 *
 ******************************/

var tsc_screen=function(parent){
	/************
	 *  VARS INIT
	 ***********/
  this.version='0.0.1';
	//uSED
	this.domObj=false;//объект
	this.canvas=[];   //Массив ссылок на дом объекты канвасов {dom:..., ctx:...} ключи - canvasID
	
	this.errors=[];   //Буффер ошибок
	this.zoom=1;   		//Кооф. масштаба
	/************
	 *  constructor - build and prepare self nesessaries
	 ***********/
	this.editor=parent;
	this.init=function(){
	  var t=document.getElementById( this.editor.config.screenId );
	  if( typeof(t)=='undefined' || !t ){
			t = document.createElement( 'div' );
			if(!t) return false;
		  t.style.width = this.editor.project.data.width		+'px';
		  t.style.height = this.editor.project.data.height	+'px';
		  t.id = this.parent.config.screenId;
		  document.body.appendChild( t );
		}
		this.domObj=t;
		if(typeof(this.domObj)=='undefined' || !this.domObj) return this.err('DOM object for Screen undefined');
		return true;
	}
	if(!this.init()) return this.err('init error');
	
	this.err=function(txt){
	  this.errors.push(txt);
	  return false;
	}
	/***********************************
	 *  project init method
	 ************************************/
	this.init_project=function(){
	  this.domObj.style.width		=	this.editor.project.data.width		+'px';
	  this.domObj.style.height	=	this.editor.project.data.height	+'px';
		this.apply();
	  return true;
	}
	
	this.apply=function(){
	  /*
    for(var i=0; i<this.canvas.length; i++){
      if( typeof(this.canvas[i])=='undefined' ) continue;
      var cvs=this.create_canvas(this.canvas[i]);
      if(!cvs) return false;
			cvs.dom.style.display='none';
		}
		*/
		//this.show_canvas_col( this.editor.current.layerNum );
		return true;
	}

	/***********************************
	 *  return canvas context by ID, ( create if not exist )
	 ************************************/
	this.get_canvas=function(canvasId,mode){
	  if( typeof(mode)=='undefined' ) mode='ctx';
	  if( typeof(canvasId)=='undefined' )	return false;
//console.log(this.canvas);
		if( typeof(this.canvas[ canvasId ])=='undefined' )	return false;

	  //Показать нужные
	  this.show_canvas( canvasId );
	  switch(mode){
	    case 'object':return this.canvas[ canvasId ];
	    case 'dom':return this.canvas[ canvasId ].dom;
	    default: return this.canvas[ canvasId ].ctx;
		}
	}
	
	this.create_canvas=function(canvasId){
	  if(typeof(canvasId)=='undefined'){
	    canvasId=this.canvas.length;
		}
	  var tst=document.getElementById('canvas_'+canvasId);
		if( tst ){
		   return {
		    id:  canvasId,
		    dom: tst,
		    ctx: tst.getContext( '2d' )
			};
		}
    var t = document.createElement( 'canvas' );
	  t.setAttribute( 'width',	this.editor.project.data.width		);
	  t.setAttribute( 'height',	this.editor.project.data.height	);
	  t.id = 'canvas_'+canvasId;
	  t.style.display = 'none';
	  t.style.position = 'absolute';
	  t.style.top = 0;
	  t.style.left = 0;
	  this.domObj.appendChild(t);
	  this.canvas[canvasId]={
	    id:		canvasId,
	    dom:	t,
	    ctx:	t.getContext( '2d' )
		};
	  return this.canvas[canvasId];
	}
	
	this.remove_canvas=function(canvasId){
		if(!this.canvas[canvasId]) return true;
		
		this.canvas[canvasId].dom.remove();
		delete this.canvas[canvasId];
		console.log(this.canvas);
		return true;
	}
	
	this.remove_frame=function(frameNum){
	  var frameId=this.editor.project.data.frameOrd[frameNum];
		if( !this.editor.project.data.table[frameId] ) return true;
		for( var i=0; i<this.editor.project.data.table[frameId].length; i++ )
		  this.remove_canvas( this.editor.project.data.table[frameId][i] );
		  
		return true;
	}
	this.remove_layer=function(layerNum){
	  var layerId=this.editor.project.data.layerOrd[layerNum];
		for( var i=0; i<this.editor.project.data.table.length; i++ )
		  this.remove_canvas( this.editor.project.data.table[i][layerId] );

		return true;
	}
	
	this.show_canvas=function(canvasId){
	  //console.log( this.canvas );
	  //console.log( this.editor.project.data.frameOrd );
   	//console.log(canvasId);
   	
	  for( var i=0; i<this.canvas.length; i++ ){
	    if( typeof(this.canvas[i])=='undefined' ) continue;
	    this.canvas[ i ].dom.style.display= (i==canvasId)?'inherit':'none';
	    //if( !this.canvas[ this.editor.project.data.canvas[i] ] ) continue;
			//this.canvas[ this.editor.project.data.canvas[i] ].dom.style.display= (i==canvasId)?'inherit':'none';
		}
	}
	this.show_canvas_col=function(colNum){
	  var fid=this.editor.project.data.frameOrd[colNum];
	  if( typeof(this.editor.project.data.table[fid])=='undefined' )	return false;

	  //for( var i=0; i<this.editor.project.data.table[fid].length; i++ ){
	  for( var i=0; i<this.editor.project.data.layerOrd.length; i++ ){
	    if( !this.editor.layers.visibility( this.editor.project.data.layerOrd[i] ) ) continue;
	    var lid=this.editor.project.data.layerOrd[i];
	    var id=this.editor.project.data.table[fid][lid];
	    this.canvas[ id ].dom.style.display='inherit';
		}
	}
}